(function($) {
/*whatever jQuery stuff*/
	$(function(){
		$('.bttnDrop').click(function(){
			$('ul.artistType li').toggle();
			if($('ul.artistType').hasClass('ulClick')){
				$('ul.artistType').removeClass('ulClick');
			}
			else{
				$('ul.artistType').addClass('ulClick');
			}
		});
		
		$('.bttDrop').click(function(){
			$('ul.selectType li').toggle();
			if($('ul.selectType').hasClass('active')){
				$('ul.selectType').removeClass('active');
			}
			else{
				$('ul.selectType').addClass('active');
			}
		});

		$('input#custom_search_keyword').keyup(function(e) {
			var txt = $.trim($(this).val());
			
			if(txt != "" && e.which != 13) {
			$.post(web_path+'ajax_search',{txt:txt},function(data) {
	          if(data != "<ul></ul>" && data != "") {
	            $('#search_autocomplete').html(data).show();
	          }else {
	            $('#search_autocomplete').hide();
	          }
	       	});
	       }else {
	      	$('#search_autocomplete').hide();
	       }
		});

		$('input#custom_search_keyword').focus(function(e) {
			var txt = $.trim($(this).val());
			if(txt == '' || txt == 'ค้นหาศิลปิน') {
			$(this).val('');
	      }
		});

	   $('input#custom_search_keyword').keypress(function(event){
	      if(event.keyCode == 13){
	          var txt = $.trim($('input#custom_search_keyword').val());
	         if(txt != '' ) {
	            customSearch(txt); 
	         }
	         return false;      
	      }
	   });

	    $('#custom-search-submit').click(function() {
      		var txt = $.trim($('input#custom_search_keyword').val());
      		if(txt != '' ) {
         		customSearch(txt);
      		}
      		return false;
   		});
		
		$('li.autoCompleteItem a').live('click',function(e) {
			var txt = $(this).attr('title');
      		$('input#custom_search_keyword').val(txt);
      		$('#custom_search_form input').attr('disabled','disabled');
      			customSearch(txt);
   		});

		$('.listType_1').click(function(){
      		menu_change(1);
			$('#type_1').removeClass('hidden');
			$('#type_2').addClass('hidden');
			$('#type_3').addClass('hidden');
	  	});
		$('.listType_2').click(function(){
      		menu_change(2);
			$('#type_2').removeClass('hidden');
			$('#type_1').addClass('hidden');
			$('#type_3').addClass('hidden');
	  	});
		$('.listType_3').click(function(){
      		menu_change(3);
			$('#type_3').removeClass('hidden');
			$('#type_2').addClass('hidden');
			$('#type_1').addClass('hidden');
	  	});
		
		$('#artistSearchText').keypress(function(event){
			if(event.keyCode == 13){
				var txtSearch = $.trim($(this).val());
				if(txtSearch != ""){
					artist_search(txtSearch);
				}
				return false;
			}			
		});			
	});
	
})(jQuery);

/*Artist Search*/
function artist_search(txt)
{
	if(txt.trim() != ""){
		window.location.href = 'http://artist.you2play.com/search?text='+txt;
	}
}

/*Vote Artist*/
function voteArtist(e,artist_id,score){
	$.ajax({
		type:'post',
		url:web_path+'ajax_vote_artist',
		data:({
			artist_id:artist_id,
			score:score
		}),
		dataType:'json',
		async:false
	}).done(function(data){
		alert(data.msg);
		if(data.avg != '' && data.people != ''){
			$(e).parent().find('.voteAvg').html(data.avg);
			$(e).parent().find('.votePeople').html(data.people);
		}
	});
}
/*Go to Tab*/
function goTab(id,e)
{	
	$(e).closest('ul').find('a').removeClass('active');
	$(e).addClass('active');
	$(''+id+'').parent().children().hide();
	$(''+id+'').show();
}

function customSearch(txt)
{
   $('#search_autocomplete').hide();
   $.ajax({
   		type:"GET",
		url:web_path+'ajax_search_result',
		data:{txt:txt}
   }).done(function(data){
   		$('#ajax_pager').html(data);
	 	$('.pager a').click(function(){
			getAjaxPager(this);
			return false;
		});
      $('#search_autocomplete').hide();
      $('#custom_search_form input').removeAttr('disabled');
   });
}

function menu_change(ch_mn)
{	
	$.ajax({
		type:'post',
		url:web_path+'ajax_set_cookies',
		data:{ch_mn:ch_mn}
	});
	
   $('.listType_1').addClass('chageBg1');
   $('.listType_2').addClass('chageBg2');
   $('.listType_3').addClass('chageBg3');
   $('.listType_'+ch_mn+'').removeClass('chageBg'+ch_mn+'');
}